package projectx.core.common.element;

import java.util.regex.Pattern;

import projectx.core.base.ProjectXMessage;
import projectx.util.StringUtil;

public class Element {

    //--------------------------------------------------------------------------
    //   S T A T I C   M E M B E R S
    //
    //--------------------------------------------------------------------------
    private static final String     REGEX_DECIMAL = "[\\-]?+[0-9]+[\\.]?+[0-9]";

    private static final Pattern    _patternAscii      = Pattern.compile( "[\\p{Graph}]*" );
    private static final Pattern    _patternAlnum      = Pattern.compile( "[a-zA-Z0-9]*" );
    private static final Pattern    _patternDigit      = Pattern.compile( "[0-9]*" );
    private static final Pattern    _patternInteger    = Pattern.compile( "[\\-]?+[0-9]+" );
    private static final Pattern    _patternDecimal    = Pattern.compile( REGEX_DECIMAL + "{0,2}" );
    private static final Pattern    _patternNumHypn    = Pattern.compile( "[0-9\\-]*" );
    private static final Pattern    _patternMailChar   = Pattern.compile( "[\\w\\.\\-@]*" );
    private static final Pattern    _patternMailAddr   = Pattern.compile( "[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+" );


    public static ProjectXMessage checkFixedLength( String name, String value, int len ){

        if ( getByteLength( value ) != len ){
            return new ProjectXMessage( "E0320", name, new Integer( len ) );
        }

        return null;
    }

    public static ProjectXMessage checkMaxLength( String name, String value, int len ){

        if ( getByteLength( value ) > len ){
            return new ProjectXMessage( "E0321", name, new Integer( len ) );
        }

        return null;
    }

    public static ProjectXMessage checkLength( String name, String value, int len, boolean fixedLength ){

        return  fixedLength ? checkFixedLength( name, value, len ) : checkMaxLength( name, value, len );
    }

    public static ProjectXMessage checkAscii( String name, String value, int len, boolean fixedLength ){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage    ret = _patternAscii.matcher( value ).matches() ? null : new ProjectXMessage( "E0337", name );

        if ( ret != null ){
            return ret;
        }

        return checkLength( name, value, len, fixedLength );
    }

    public static ProjectXMessage checkAsciiAlphaNumeric( String name, String value, int len, boolean fixedLength ){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage    ret = _patternAlnum.matcher( value ).matches() ? null : new ProjectXMessage( "E0331", name );

        if ( ret != null ){
            return ret;
        }

        return checkLength( name, value, len, fixedLength );
    }
    
    public static ProjectXMessage checkAsciiNumeric( String name, String value, int len, boolean fixedLength ){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage    ret = _patternDigit.matcher( value ).matches() ? null : new ProjectXMessage( "E0332", name );
        if ( ret != null ){
            return ret;
        }

        return checkLength( name, value, len, fixedLength );
    }

    public static ProjectXMessage checkAsciiInteger( String name, String value ){

        if ( getLength( value ) == 0 ) return null;

        return _patternInteger.matcher( value ).matches() ? null : new ProjectXMessage( "E0335", name );
    }

    public static ProjectXMessage checkAsciiDecimal( String name, String value ){

        if ( getLength( value ) == 0 ) return null;

        return _patternDecimal.matcher( value ).matches() ? null : new ProjectXMessage( "E0336", name );
    }

    public static ProjectXMessage checkAsciiNumericHyphen( String name, String value, int maxLen ){

        return checkAsciiNumericHyphen( name, value, maxLen, false );
    }

    public static ProjectXMessage checkAsciiNumericHyphen( String name, String value, int len, boolean fixedLength ){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage    ret = _patternNumHypn.matcher( value ).matches() ? null : new ProjectXMessage( "E0333", name );
        if ( ret != null ){
            return ret;
        }

        return checkLength( name, value, len, fixedLength );
    }

    public static ProjectXMessage checkTelNo( String name, String value, int len ){

        return checkAsciiNumericHyphen( name, value, len );
    }

    public static ProjectXMessage checkMailAddress( String name, String value, int maxLen ){

        if ( getLength( value ) == 0 ) return null;

        ProjectXMessage   ret = _patternMailChar.matcher( value ).matches() ? null : new ProjectXMessage( "E0334", name );
        if ( ret != null ){
            return ret;
        }

        ret = isMailAddress( value ) ? null : new ProjectXMessage( "E0330", name );
        if ( ret != null ){
            return ret;
        }

        return checkMaxLength( name, value, maxLen );
    }

    public static boolean isMailAddress( String value ){

        return _patternMailAddr.matcher( value ).matches();
    }

    public static ProjectXMessage checkDuplicate( String name, Object[] value ){

        for ( int i = 0 ; i < value.length ; i++ ){

            for ( int j = 0 ; j < value.length ; j++ ){

                if ( i == j )   continue;

                if ( value[i].equals( value[j] ) ){
                    return new ProjectXMessage( "E0325", name );
                }
            }
        }

        return null;
    }

    public static ProjectXMessage checkFromTo( String name, String fromValue, String toValue ){

       if ( getLength( fromValue ) == 0 && getLength( toValue ) != 0) {
           return  new ProjectXMessage( "E0311", name );
       }
       if ( getLength( fromValue ) != 0 && getLength( toValue ) != 0) {
           if ( getLength( fromValue ) != getLength( toValue ) ) {
               if ( getLength( fromValue ) > getLength( toValue ) ) {
                   toValue = StringUtil.str2fixedLen( toValue, getLength( fromValue ), ' ', false);
               } else if ( getLength( fromValue ) < getLength( toValue ) ) {
                   fromValue = StringUtil.str2fixedLen( fromValue, getLength( toValue ), ' ', false);
               }
           }
           return fromValue.compareTo( toValue ) <= 0 ? null : new ProjectXMessage( "E0345", name );
       }
       return null;
    }

    public static int getLength( String s ){

        return ( s == null ) ? 0 : s.length();
    }

    public static int getByteLength( String s ){

        return StringUtil.length( s );
    }


    //--------------------------------------------------------------------------
    //   C O N S T R U C T O R S
    //
    //--------------------------------------------------------------------------
    //private Element(){}
}
